package com.dongdongshop.serviceimpl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.dongdongshop.pojo.Cart;
import com.dongdongshop.pojo.TbItem;
import com.dongdongshop.pojo.TbOrderItem;
import com.dongdongshop.service.CartService;
import com.dongdongshop.service.TbItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
@org.springframework.stereotype.Service
public class CartServiceImpl implements CartService {
    @Reference
    private TbItemService tbItemService;
    @Autowired
    private RedisTemplate redisTemplate;
    //往购物车内加入商品
    @Override
    public List<Cart> addCart(List<Cart> carts, Long itemId, Integer num) {
        //1.根据商品itemId查询到商品
        TbItem tbItem = tbItemService.selectItemById(itemId);
        //2.根据itemId查找到的商家
        String sellerId = tbItem.getSellerId();
        String seller = tbItem.getSeller();
        //3.判断购物车有没有这个商家
        Cart cart = findCartBySellerId(carts, seller);
            if (cart != null) {//4.有
                //4.1判断当前购物车有没有该商家当前商品
                List<TbOrderItem> list = cart.getList();
                TbOrderItem orderItem = findTbOrderItemByItemId(list, itemId);
                if (orderItem != null) {//4.2有
                        //4.2.1有的话在原来的商品基础上数量增加价格改一下
                        orderItem.setNum(orderItem.getNum() + num);
                        orderItem.setTotalFee(new BigDecimal(tbItem.getPrice().doubleValue() * orderItem.getNum()));
                        //当num(商品数量)<=0时 我们将购物车中的商品删掉
                        if (orderItem.getNum() <= 0){
                            list.remove(orderItem);
                        }
                        if (cart.getList().size() <= 0){
                            carts.remove(cart);
                        }
                    } else {//4.3没有
                        //4.3.1没有就将当前商品放进对应商家购物车内
                        TbOrderItem orderItem1 = new TbOrderItem();
                        orderItem1.setItemId(itemId);
                        orderItem1.setPrice(tbItem.getPrice());
                        orderItem1.setNum(num);
                        orderItem1.setGoodsId(tbItem.getGoodsId());
                        orderItem1.setPicPath(tbItem.getImage());
                        orderItem1.setSellerId(sellerId);
                        orderItem1.setTitle(tbItem.getTitle());
                        orderItem1.setTotalFee(new BigDecimal(tbItem.getPrice().doubleValue() * num));
                        list.add(orderItem1);
                    }
            } else {//5.没有
                //5.1没有的话创建一个cart（创建一个商家）
                Cart cart1 = new Cart();
                //5.2 将对应该商家的商品放入cart并放进list
                List<TbOrderItem> list = new ArrayList<>();
                TbOrderItem orderItem1 = new TbOrderItem();
                orderItem1.setItemId(itemId);
                orderItem1.setPrice(tbItem.getPrice());
                orderItem1.setNum(num);
                orderItem1.setGoodsId(tbItem.getGoodsId());
                orderItem1.setPicPath(tbItem.getImage());
                orderItem1.setSellerId(sellerId);
                orderItem1.setTitle(tbItem.getTitle());
                orderItem1.setTotalFee(new BigDecimal(tbItem.getPrice().doubleValue() * num));
                list.add(orderItem1);
                cart1.setSellerId(sellerId);
                cart1.setSellerNmame(seller);
                cart1.setList(list);
                carts.add(cart1);
            }
        return carts;
    }
    //根据商家id查询购物车对象
    public Cart findCartBySellerId(List<Cart> carts, String seller){
        for (Cart cart : carts) {
            if (Objects.equals(cart.getSellerId(), seller)) {
                return cart;
            }
        }

        return null;
    }

    //根据itemId查询购物车明细表中明细数据
    public TbOrderItem findTbOrderItemByItemId(List<TbOrderItem> list,Long itemId){
        for (TbOrderItem orderItem : list) {
            if (Objects.equals(orderItem.getItemId(), itemId)) {
                return orderItem;
            }
        }
        return null;
    }

    //将购物车数据放入redis当中
    @Override
    public void addCartListToRedis(List<Cart> cartList, Long id) {
        redisTemplate.opsForHash().put("cartList",id,cartList);
    }

    //从redis中获取购物车数据
    @Override
    public List<Cart> findRedisCartList(Long id) {
        List<Cart> cartList = (List<Cart>) redisTemplate.opsForHash().get("cartList", id);
        if (cartList == null){
            cartList = new ArrayList<>();
        }
        return cartList;
    }

    //合并购物车
    @Override
    public List<Cart> merageCartList(List<Cart> reidsCartList, List<Cart> cookieCartList) {
        for (Cart cart : cookieCartList) {
            List<TbOrderItem> list = cart.getList();
            for (TbOrderItem orderItem : list) {
                Long itemId = orderItem.getItemId();
                reidsCartList = addCart(reidsCartList, itemId, orderItem.getNum());
            }
        }
        return reidsCartList;
    }
}
